package cn.com.flever.handler;

import com.alibaba.fastjson.JSONObject;
import io.netty.channel.Channel;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.UUID;

public class MobileHandler extends AbstractHandler{


    @Override
    public JSONObject clientUp(JSONObject req, Channel channel) {
        //获取PC端的token
        String uuid=req.getString("uuid");
        JSONObject res=new JSONObject();
        res.put("uuid", uuid);
        res.put("code","200");
        res.put("message","Mobile端上线成功");
        return res;
    }

    @Override
    public JSONObject dispacher(JSONObject req, Channel channel) {
        try {
            String methodName=req.getString("method");
            Method method=this.getClass().getMethod(methodName,JSONObject.class,Channel.class);
            JSONObject res= (JSONObject) method.invoke(this,req,channel);
            return res;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public JSONObject run(JSONObject req, Channel channel)
    {
        //获取pc唯一标识
        String token=req.getString("token");
        //获取pc端channel
        Channel pcChannl = PCHandler.clientMap.get(token);
        JSONObject res=new JSONObject();
        if(pcChannl!=null)
        {
            //发送命令
            pcChannl.writeAndFlush(req);
            String uuid=req.getString("uuid");
            res.put("uuid", uuid);
            res.put("code","200");
            res.put("message","消息已发送");
        }
        else{
            String uuid=req.getString("uuid");
            res.put("uuid", uuid);
            res.put("code","404");
            res.put("message","PC端不存在");
        }
        return res;
    }
}
